<!DOCTYPE html>
<html>

	<head>
		<title>登录 —【在线订餐系统】</title>
		<meta http-equiv="content-type" content="text/html;charset=utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=7">
		<link rel="stylesheet" type="text/css" href="/css/admin/admin.css">
	</head>

	<body class="body">
		<br>
		<div class="title">
			欢迎使用后台管理系统
		</div>
		<br>
		<div class="login">
			<h2>登录系统</h2>
			<span id="error" class="error"></span>
			<form method="post" action="/">
				<br>
				<span>用户：</span><input type="text" id="username" name="username" value="admin">
				<span class="error">*</span>
				<br><br>
				<span>密码：</span><input type="password" id="password" name="password" value="admin">
				<span class="error">*</span>
				<br><br>
				<input type="button" id="submit_btn" value="提交">
			</form>
		</div>

		<script src="/js/common/jquery-1.12.4.min.js"></script>
		<script>
			$(document).keydown(function(event) {
				if(event.keyCode == 13) {
					checkForm();
				}
			});
			
			$("#submit_btn").click(function () {
				checkForm()
			})

			function checkForm() {
				if($('#submit_btn').attr('disabled') == true) {
					return false;
				}

				var name = $("#username").val().trim();
				var pwd = $("#password").val().trim();

				if(name == "") {
					$("#error").html('请输入帐号');
					return false;
				}

				if(pwd == "") {
					$("#error").html("请输入密码");
					return false;
				}

				if(pwd.length < 1) {
					$("#error").html('密码格式有误');
					return false;
				}

				$("#error").html("正在登录，请稍后...");
				$('#submit_btn').attr('disabled', true);

				$.ajax({
					type: 'post',
					url: "/admin/login",
					timeout : 3000, 
					dataType: 'json',
					data: {
						'username': name,
						'password': pwd
					},
					success: function(result) {
						console.log(result);
						$("#error").html(result.msg);
						$('#submit_btn').removeAttr('disabled');
						if(result.err == 0 && result.data.url) {
							window.location.href = result.data.url;
						}
					},
					error: function(error) {
						console.log(error);
						$('#submit_btn').removeAttr('disabled');
						$("#error").html("请求出错，请稍后再试");
					}
				});
			}
		</script>
	</body>

</html>